package com.czb.servlet;

import com.czb.service.RoleService;
import com.czb.service.RoleServiceImpl;
import com.czb.util.PageBean;
import com.czb.util.ResultJson;
import com.czb.util.Utils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@WebServlet("/RoleServlet")
public class RoleServlet extends HttpServlet {
    RoleService roleService = new RoleServiceImpl();
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String action = req.getParameter("action");
        if(action.equals("findAll")){
            findAll(req,resp);
        }else if(action.equals("add")){
            add(req,resp);
        }else if(action.equals("update")){
            update(req,resp);
        }else if(action.equals("delete")){
            delete(req,resp);
        }else if(action.equals("findById")){
            findById(req,resp);
        }

    }

    private void findById(HttpServletRequest req, HttpServletResponse resp) {
        Integer id = Integer.valueOf(req.getParameter("id"));
        System.out.println(id);
        String sql = "select * from role where id = ? ";
        Map map = roleService.findById(sql,id);
        ResultJson success = ResultJson.success(map);
        Utils.returnJson(success,resp);
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) {
        Integer id = Integer.valueOf(req.getParameter("id"));
        String sql = "delete from role where id = ? ";
        int i = roleService.delete(sql,id);
        if(i > 0 ){
            ResultJson success = ResultJson.success(i);
            Utils.returnJson(success,resp);
        }else {
            ResultJson error = ResultJson.error();
            Utils.returnJson(error,resp);
        }
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        Integer id = Integer.valueOf(req.getParameter("id"));
        String roleType = req.getParameter("roleType");
        String state = req.getParameter("state");
        Object[] obj = {roleType,state,id};
        String sql ="update role set roleType = ?,state = ? where id = ?";
        int i = roleService.update(sql,obj);
        if(i > 0 ){
            ResultJson success = ResultJson.success(i);
            Utils.returnJson(success,resp);
        }else {
            ResultJson error = ResultJson.error();
            Utils.returnJson(error,resp);
        }
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        String roleType = req.getParameter("roleType");
        String state = req.getParameter("state");
        Object[] obj = {roleType,state};
        String sql = "insert into role values(null,?,?)";
        int i =  roleService.add(sql,obj);
        if(i > 0 ){
            ResultJson success = ResultJson.success(i);
            Utils.returnJson(success,resp);
        }else {
            ResultJson error = ResultJson.error();
            Utils.returnJson(error,resp);
        }
    }

    private void findAll(HttpServletRequest req, HttpServletResponse resp) {
        Integer page = req.getParameter("page") != null ? Integer.valueOf(req.getParameter("page")) : 1;
        Integer limit = req.getParameter("limit") != null ? Integer.valueOf(req.getParameter("limit")) : 10;
        String  roleType =req.getParameter("roleType");
        System.out.println(page+limit+roleType);
        String sql = "select * from role";
        PageBean<Map> pageBean = new PageBean<Map>();
        if(roleType == null || roleType ==""){
            pageBean =  roleService.findAll(page,limit,sql);
        }else if(roleType != null || roleType !=""){
            pageBean = roleService.findAll(page,limit,sql.concat(" where roleType like ?"),"%"+roleType+"%");
        }
        System.out.println(pageBean);
        ResultJson success = ResultJson.success(pageBean);
        Utils.returnJson(success,resp);
    }
}
